package com.byit.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.builder.MapBuilder;
import com.byit.constant.DefaultValue;
import com.byit.domain.AppAddress;
import com.byit.domain.AppApplication;
import com.byit.enums.PromptInfo;
import com.byit.exception.ServiceException;
import com.byit.msg.MessageResult;
import com.byit.service.IAppAddressService;
import com.byit.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 常用地址表 前端控制器
 * </p>
 *
 * @author puyunfeng
 * @since 2019-01-17
 */
@RestController
@RequestMapping("/api/appAddress")
public class AppAddressController {

    @Autowired
    private IAppAddressService appAddressService;


    /**
     * 地址列表
     */
    @GetMapping("/list")
    public Object list(@RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
                       @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit) {
        Page<AppApplication> pageInfo = new Page<>(page, limit);
        if (StringUtils.isBlank(keyword)) { // 数据校准
            keyword = "";
        }
        Page<JSONObject> list = appAddressService.selectAddressList(pageInfo, MapBuilder.newMap(1)
                .setParam("keyword", '%' + StringUtil.decodeSpecialCharsWhenLikeUseBackslash(keyword) + '%')
                .ok());

        return MessageResult.success(list);
    }


    /**
     * 新增或者编辑
     */
    @PostMapping("/edit")
    public Object edit(
            @Valid AppAddress address,
            Errors errors) {

        // 表单校验
        if (errors.hasErrors()) {
            return MessageResult.warpper(PromptInfo.ERROR_DATA);
        }

        try {
            appAddressService.saveOrUpdate(address);
        }catch (ServiceException e) {
            return MessageResult.error(e);
        }

        return MessageResult.success();
    }



    /**
     * 删除
     */
    @PostMapping("/delete")
    public MessageResult delete(
            @RequestParam("userId") Long userId,
            @RequestParam("id") Long addressId) {
        // 标记删除
        appAddressService.deleteByIdAndUserId(addressId, userId);
        return MessageResult.success();
    }


    /**
     * 获取省份
     */
    @GetMapping("/province")
    public MessageResult provinceList() {
        List<JSONObject> list = appAddressService.selectProvince();
        return MessageResult.success(list);
    }


    /**
     * 获取城市
     */
    @GetMapping("/city")
    public MessageResult cityList( @RequestParam("provinceId") Long provinceId) {
        List<JSONObject> list = appAddressService.selectCity(provinceId);
        return MessageResult.success(list);
    }

    /**
     * 获取区县
     */
    @GetMapping("/region")
    public MessageResult regionList( @RequestParam("cityId") Long cityId) {
        List<JSONObject> list = appAddressService.selectRegion(cityId);
        return MessageResult.success(list);
    }
}
